var Permissions = []

function updateMenu() {
    var menu1 = document.getElementById('menu1');
    var menu2 = document.getElementById('menu2');
    var menu3 = document.getElementById('menu3');
    var menu4 = document.getElementById('menu4');

    // 首先隐藏所有菜单
    menu1.style.display = 'none';
    menu2.style.display = 'none';
    menu3.style.display = 'none';
    menu4.style.display = 'none';

    // 根据权限显示相应的菜单
    if (Permissions.includes('1')) {
        menu1.style.display = '';
    }
    if (Permissions.includes('2')) {
        menu2.style.display = '';
    }
    if (Permissions.includes('3')) {
        menu3.style.display = '';
    }
    if (Permissions.includes('4')) {
        menu4.style.display = '';
    }
    if (Permissions[0] == 9) {
        menu1.style.display = '';
        menu2.style.display = '';
        menu3.style.display = '';
        menu4.style.display = '';
    }
}

window.addEventListener('message', function(event) {
    Permissions = event.data
    if (!Array.isArray(Permissions)) {
        Permissions = [Permissions]; // 确保 Permissions 始终是一个数组
    }
    updateMenu()
});

function checkLoginState() {
    var isLoggedIn = sessionStorage.getItem('isLoggedIn')
    if(isLoggedIn) {
        document.getElementById('loginDiv').style.display = 'none'
        Permissions = sessionStorage.getItem('userPermissions')
        updateMenu()
    } else {
        document.getElementById('loginDiv').style.display = 'block'
    }
}

checkLoginState()